﻿using System.Linq;
using Newtonsoft.Json.Linq;

namespace PlurkApi
{
    /// <summary>
    /// my profile
    /// </summary>
    public class ownProfile
    {
        /// <summary>
        /// the number of my fans
        /// </summary>
        public int fans_count{ get; set;}

        /// <summary>
        /// the number of my friends
        /// </summary>        
        public int friends_count { get; set; }

        /// <summary>
        /// number of alerts that are unread
        /// </summary>
        public int alerts_count { get; set; }              

        /// <summary>
        /// number of plurks that are unread
        /// </summary>
        public int unread_count { get; set; }

        public string privacy { get; set; }

        public bool has_read_permission { get; set; }

        public user_info user_info { get; set; }
        public user_infos plurks_users { get; set; }        
        public plurks plurks { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ownProfile"/> class.
        /// </summary>
        /// <param name="jsonString">The json string.</param>
        public ownProfile(string jsonString)
        {
            var obj = JObject.Parse(jsonString);
            if(obj.Count <= 0){ return;}

            fans_count = (obj["fans_count"] == null || obj["fans_count"].ToString() == "null") ? 0 : (int)obj["fans_count"];
            alerts_count = (obj["alerts_count"] == null || obj["alerts_count"].ToString() == "null") ? 0 : (int)obj["alerts_count"];
            friends_count = (obj["friends_count"] == null || obj["friends_count"].ToString() == "null") ? 0 : (int)obj["friends_count"];
            unread_count = (obj["unread_count"] == null || obj["unread_count"].ToString() == "null") ? 0 : (int)obj["unread_count"];

            privacy = (obj["privacy"] == null || obj["privacy"].ToString() == "null") ? string.Empty : (string)obj["privacy"];

            has_read_permission = (obj["has_read_permission"] == null || obj["has_read_permission"].ToString() == "null") ? false : (bool)obj["has_read_permission"];

            user_info = (obj["user_info"] == null) ? null : new user_info(obj["user_info"].ToString());            

            if (obj["plurks"] != null)
            {
                var tplurks = new plurks();
                var count = obj["plurks"].Count();
                for (var i = 0; i < count; i++)
                {
                    var dataObj = obj["plurks"][i].ToString();
                    tplurks.Add(new plurk(dataObj));
                }
                plurks = tplurks;
            }
        }
    }
}
